﻿package me.hvs.farmerdroid;

import java.util.Date;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class FarmService extends Service {

	private boolean threadDisable = false;
	private ArrayList<FarmField> myFields = new ArrayList<FarmField>();
	private final IBinder mBinder = new LocalBinder();
	public class LocalBinder extends Binder {
		FarmService getService() {
			return FarmService.this;
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		Log.d("FarmService", "onBind()");
		return mBinder;
	}

	@Override
	public void onCreate() {
		Log.d("FarmService", "onCreate()");
		super.onCreate();
		Thread thread = new Thread() {
			@Override
			public void run() {
				while (!threadDisable) {
					try {
						Log.d("FarmService", "in timer.");
						updateFieldState();
						Thread.sleep(1000 * 10);
					} catch (InterruptedException e) {
					}
				}
			}
		};
		thread.start();
	}

	@Override
	public void onDestroy() {
		Log.d("FarmService", "onDestroy()");
		super.onDestroy();
		threadDisable = true;
	}
	
	public boolean updateFieldState() {
		ArrayList<FarmField> fields = new ArrayList<FarmField>();
		
		String html = "";
		try {
			html= Util.getHtml("http://mcapp.z.qq.com/nc/cgi-bin/wap_farm_index?sid=FVn6Ou2QcsWgQ87Nd3qydrWKHLlv3SQ%2F30f7ac7d0201%3D%3D&all=1&B_UID=0&money=2937595&name=&g_ut=2");
//			html= Util.getHtml("http://mcapp.z.qq.com/nc/cgi-bin/wap_farm_fish_index?sid=FVn6Ou2QcsWgQ87Nd3qydrWKHLlv3SQ%2F30f7ac7d0201%3D%3D&g_ut=2&B_UID=2320269472&money=8758&name=%E6%B7%A1%E7%84%B6%E5%B0%8F%E5%A5%BD%E9%85%92");
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		Log.d("FarmService", "html length : " + html.length());

		Pattern p1 = Pattern.compile("(\\d*?)小时(\\d*?)分后成熟");
		Pattern p2 = Pattern.compile("<a href=\"([\\S\\s]*?)\">([\\S]*?)</a>");
        Pattern p = Pattern.compile("<div class=\"spacing-3 border-btm\">\\s*?<p class=\"tabs-1\">\\d+\\.\\s*?(?:\\((\\S*?)\\))?\\s*?(?:土地\\d+)?\\s*?(\\S*?)\\s*?\\(\\s*?(\\S*?)\\s*?\\)\\s*?</p>\\s*?<p class=\"tabs-1\">[\\s\\S]*?<span class=\"txt-fade\">([\\s\\S]*?)</span>([\\s\\S]*?)</p>");
        Matcher m = p.matcher(html);
        while(m.find()) {
            // System.out.println(m.group(1) == null ? "NULL" : m.group(1));
            // System.out.println(m.group(2));
            // System.out.println(m.group(3));
            // System.out.println(m.group(4));
            
        	String state = "";
        	Date time = new Date();
    		Matcher m1 = p1.matcher(m.group(4));
    		if (m1.find())
                time = new Date(time.getTime() + 60000 * (60 * Long.valueOf(m1.group(1)) + Long.valueOf(m1.group(2))));
        	if (m.group(1) != null)
        		state += m.group(1) + "土地 ";
    		state += m.group(3) + " ";
    		state += m.group(4) + " ";

    		Matcher m2 = p2.matcher(m.group(5));
    		while (m2.find()) {
    			// Log.d("FarmService:updateFieldState", m2.group(2) + ' ' + m2.group(1));
    			state += '可' + m2.group(2) + ' ';
    			
    			if (m2.group(2).equals("浇水") ||
    					m2.group(2).equals("除草") ||
    					m2.group(2).equals("收获") ||
    					m2.group(2).equals("铲除") ||
    					m2.group(2).equals("杀虫")) {
    				Log.d("FarmService:updateFieldState", m2.group(2) + ' ' + m2.group(1));
    				String url = m2.group(1).replace("&amp;", "&");
    				try {
						String ret= Util.getHtml(url);
						
					} catch (Exception e) {
						e.printStackTrace();
					}
    			}
    		}
    		
            // System.out.println(state);
    		fields.add(new FarmField(m.group(2), time, state));
        }

		myFields = fields;
		return true;
	}
	
	public ArrayList<FarmField> getFieldState() {
		return myFields;
	}
}
